/**
* @file 分类
* @author 张超
* @created 2023/6/30 08:00
* @updated 2023/6/30 18:00
*/
<template>
	<view class="kind">
		<u-navbar :is-back="false" :border-bottom="false" title-size="35" title="分类"></u-navbar>
		<u-search placeholder="请输入关键字" height="58" @blur="blur" shape="square" v-model="keyword"
			:show-action="false"></u-search>
		<view class="kind-cont">
			<ul class="kindLeft">
				<li v-for="(item,index) in leftList" :key="item.id" @click="cut(item.id)">
					<text v-show="currentId==item.id"></text>
					<text :class="currentId==item.id?'active':''">{{item.name}}</text>
				</li>
			</ul>
			<view class="kindRight">
				<image :src="img"></image>
				<text>{{name}}</text>
				<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="lower">
					<ul v-if="rightList.length>0">
						<li v-for="(item1,index1) in rightList" :key="index1" @click="goProduct(item1.id)">
							<image :src="item1.goodsimage_text"></image>
							<text>{{item1.goodsname}}</text>
						</li>
					</ul>
					<block v-else>
						<u-empty text="数据为空" mode="list"></u-empty>
					</block>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		kind,
		goods_list
	} from '@/network/api.js'
	export default {
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				keyword: '',
				current: 0,
				leftList: [],
				rightList: [],
				// 当前左侧分类id
				currentId: '',
				// 广告图
				img: '',
				page: 1
			}
		},
		onLoad() {

		},
		onShow() {
			this.kinRe()
		},
		methods: {
			// 切换
			cut(id) {
				uni.setStorage({ key: "kind_id", data: id });
				this.currentId = id
				this.rightList = []
				this.page = 1
				let filteredArray = this.leftList.filter(obj =>(obj.id === id));
				this.name=filteredArray[0].name
				this.img=filteredArray[0].gimage_text
				this.loadmoreRe(this.currentId)
			},
			// 获取左侧分类
			kinRe() {
				let data = {
					type: 'default',
					limit: 0
				}
				kind(data).then(res => {
					this.leftList = res.data
					if (uni.getStorageSync("kind_id")) {
						let kind_id = uni.getStorageSync("kind_id")
						this.cut(kind_id)
						console.log('触发')
					} else {
						if (this.leftList.length > 0) {
							this.cut(this.leftList[0].id)
						}
					}

				})
			},
			// 获取右侧分类
			loadmoreRe(id) {
				let data = {
					object: 'category',
					objectid: id,
					page: this.page,
					pageSize: 5
				}
				goods_list(data).then(res => {
					if (this.page == 1) {
						this.rightList = res.data.data
					} else {
						this.rightList = [...this.rightList, ...res.data.data]
					}
					console.log(this.rightList, '返回结果')
				})
			},
			// 搜索框失去焦点触发
			blur() {
				uni.navigateTo({
					url: '/pages/subpackage/shop/search?data=' + this.keyword
				})
			},
			// 跳转商品详情页
			goProduct(id) {
				if(uni.getStorageSync("kind_id")){
					let kind_id=uni.getStorageSync("kind_id")
					uni.setStorage({ key: "kind_id", data: kind_id });
				}
				uni.navigateTo({
					url: '/pages/subpackage/shop/productDetail?id=' + id
				})
			},
			lower: function(e) {
				console.log(e);
				this.page++
				this.loadmoreRe(this.currentId)
			},
		}
	}
</script>

<style lang="scss">
	.kind {
		.u-search {
			margin: 0 30rpx !important;
		}

		.kind-cont {
			display: flex;
			justify-content: space-between;
			margin-right: 30rpx;
			margin-top: 20rpx;

			.kindLeft {
				width: 180rpx;
				height: calc(100vh - 230rpx);
				overflow-y: scroll;
				background-color: #F6F6F6;

				>li {
					display: flex;
					align-items: center;
					height: 100rpx;
					text-align: center;
					line-height: 100rpx;
					font-size: 28rpx;

					>text:nth-child(1) {
						display: block;
						width: 5rpx;
						height: 50rpx;
						background-color: #fff;
						background: linear-gradient(to top, #E3BA5B, #B07412);
					}

					>text:nth-child(2) {
						display: block;
						width: 100%;
					}

					.active {
						background-color: #fff;
					}
				}

			}

			.kindRight {
				width: 510rpx;

				>image {
					width: 100%;
					height: 15vh;
				}

				>text {
					display: block;
					margin: 20rpx 0rpx;
				}

				.scroll-Y {
					height: calc(100vh - 570rpx);

					.uni-scroll-view {
						.uni-scroll-view {
							.uni-scroll-view-content {

								>ul {
									display: flex;
									justify-content: space-between;
									flex-wrap: wrap;
									// height: calc(100vh - 570rpx);
									// overflow-y: scroll;

									>li {
										width: 150rpx;
										height: 210rpx;
										margin-bottom: 20rpx;

										>image {
											width: 100%;
											height: 150rpx;
										}

										>text {
											display: block;
											width: 150rpx;
											font-size: 28rpx;
											white-space: nowrap;
											overflow: hidden;
											text-overflow: ellipsis;
										}
									}

									/* 如果最后一行是3个元素 */
									>li:last-child:nth-child(3n - 1) {
										margin-right: calc(33% + 4% / 3);
									}

									/* 如果最后一行是2个元素 */
									>li:last-child:nth-child(3n - 2) {
										margin-right: calc(48% + 8% / 3);
									}


									>li:active {
										color: #f7d950
									}
								}
							}
						}
					}
				}
			}
		}
	}
</style>